/**
 * Candidate number: 35834
 */
package tv.starfu.systems;

import tv.starfu.animae.Anima;
import tv.starfu.animae.Player;
import tv.starfu.animae.Terrain;
import tv.starfu.animae.Tile;
import tv.starfu.animae.TileGrid;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import com.artemis.utils.Bag;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import tv.starfu.animae.RaceTrack;
import tv.starfu.animae.StarRacer;
import tv.starfu.components.AnimaSpatial;
import tv.starfu.components.Transform;
import tv.starfu.components.level.KyberneteGrid;

/**
 * Renders all entities with AnimaSpatial and Transform Components
 * @author niadzi
 */
public class RenderSystem extends EntityProcessingSystem {
    
    private SimpleApplication app; 
    private Camera cam;
    private Node localRootNode;
    private Node level = new Node("starLevel");
    
  //  private Node renderNode;
//   // private KyberneteGrid kyberGrid;
    private BulletAppState bullet;
    
    private ComponentMapper<AnimaSpatial> animaSpatialMapper;
    private ComponentMapper<Transform> positioningMapper;
    private Bag<Anima> animae;
    
    
    public RenderSystem(SimpleApplication application, BulletAppState b, Node arcadeRoot) {
        super(AnimaSpatial.class, Transform.class);
        bullet = b;
        app = application; 
        //this.cam = sceneCam; 
        localRootNode = arcadeRoot;
        animae = new Bag<Anima>();
    }
    
    @Override
    public void initialize() {
        animaSpatialMapper = new ComponentMapper<AnimaSpatial>(AnimaSpatial.class, world);
        positioningMapper  = new ComponentMapper<Transform>(Transform.class, world);
        
        localRootNode.attachChild(level);
    }
    
    @Override
    protected void process(Entity e) {
        
        Anima anima = animae.get(e.getId());                    // Get Anima of Entity e
        Transform positioning = positioningMapper.get(e);     // Get Transform component of Entity e
        AnimaSpatial ass = animaSpatialMapper.get(e);     // Get Transform component of Entity e
        anima.render(); 
    }
    
	@Override
	protected void added(Entity e) {
		Anima anima = createAnima(e);
		if (anima != null) {
			anima.initalize();
			animae.set(e.getId(), anima);
		}
	}

	@Override
	protected void removed(Entity e) {
		animae.set(e.getId(), null);
	}

        private Anima createAnima(Entity e) {
		AnimaSpatial animaSpatial = animaSpatialMapper.get(e);
		String animaSpatialName = animaSpatial.getAnimaSpatialName();

		if ("Player".equalsIgnoreCase(animaSpatialName)) {
                    return new StarRacer(world, e, app, level);
                    //return new Player(world, e, app, cam, localRootNode, bullet);
		}
                
                else if ("Tile".equalsIgnoreCase(animaSpatialName)) {
                        System.out.println("RenderSystem: Made tile");
			return new Tile(world, e, app, localRootNode);
                }
                
                else if ("TileGrid".equalsIgnoreCase(animaSpatialName)) {
                        System.out.println("RenderSystem: Made TileGrid");
			return new TileGrid(world, e, app, localRootNode);
                }
                
                else if ("Terrain".equalsIgnoreCase(animaSpatialName)) {
                        System.out.println("Making terrain");
			return new RaceTrack(world, e, app, level);
                        //return new Terrain(world, e, app);
                }
                
		return null;
	}
}
